﻿using System;

namespace MicroContainer.CompileTime
{
   /// <summary>
   /// Unique key identyfying a type mapping.
   /// </summary>
   internal sealed class TypeMappingKey
   {
      /// <summary>
      /// Gets type which should be mapped.
      /// </summary>
      public Type From { get; private set; }
      /// <summary>
      /// Gets optional mapping name.
      /// </summary>
      public string Name { get; private set; }      

      /// <summary>
      /// Creates new <see cref="TypeMappingKey"/> instance.
      /// </summary>
      /// <param name="from">From type.</param>      
      /// <param name="name">Optional name.</param>
      public TypeMappingKey(Type from, string name)
      {
         if (from == null)
         {
            throw new ArgumentNullException("from");
         }
         From = from;         
         Name = name;
      }

      public override bool Equals(object obj)
      {
         TypeMappingKey other = obj as TypeMappingKey;
         return (other != null && From.FullName.Equals(other.From.FullName) && Name == other.Name);
      }

      public override int GetHashCode()
      {
         int code = From.FullName.GetHashCode();
         if (Name != null)
         {
            code ^= Name.GetHashCode();
         }
         return code;
      }

      public override string ToString()
      {
         return From.FullName + "[" + Name + "]";
      }
   }
}
